import React from 'react';
import type { MenuProps } from 'antd';
import {
  BranchesOutlined,
  NodeIndexOutlined,
  ProfileOutlined,
  OrderedListOutlined,
  AimOutlined,
} from '@ant-design/icons';

// Type-safe Nav config
export type LeafNavItem = { key: string; label: string; icon?: React.ReactNode };
export type ParentNavItem = {
  key: string;
  label: string;
  icon?: React.ReactNode;
  children: LeafNavItem[];
};
export type NavGroup = { type: 'group'; label: string; children: (ParentNavItem | LeafNavItem)[] };

export const navConfig: NavGroup[] = [
  {
    type: 'group',
    label: '树与二叉树',
    children: [
      {
        key: 'bst',
        label: '二叉搜索树（BST）',
        icon: React.createElement(BranchesOutlined),
        children: [
          { key: '/bst', label: '概览' },
          { key: '/bst/search', label: '700. 二叉搜索树中的搜索' },
          { key: '/bst/validate', label: '验证' },
          { key: '/bst/min-abs-diff', label: '530. 最小绝对差' },
          { key: '/bst/mode', label: '501. 二叉搜索树中的众数' },
          { key: '/bst/bst-lca', label: '235. 二叉搜索树的最近公共祖先' },
          { key: '/bst/delete', label: '450.删除二叉搜索树中的节点' },
        ],
      },
      {
        key: '/tree',
        label: '二叉树',
        icon: React.createElement(AimOutlined),
        children: [{ key: '/tree/lca', label: '二叉树最近公共祖先' }],
      },
    ],
  },
  {
    type: 'group',
    label: '链式结构',
    children: [
      {
        key: 'list',
        label: '单向链表（LinkedList）',
        icon: React.createElement(NodeIndexOutlined),
        children: [
          { key: '/list', label: '概览' },
          { key: '/list/validate', label: '验证' },
        ],
      },
    ],
  },
  {
    type: 'group',
    label: '栈与队列',
    children: [
      {
        key: 'stack',
        label: '栈（Stack）',
        icon: React.createElement(ProfileOutlined),
        children: [
          { key: '/stack', label: '概览' },
          { key: '/stack/validate', label: '验证' },
        ],
      },
      {
        key: 'queue',
        label: '队列（Queue）',
        icon: React.createElement(OrderedListOutlined),
        children: [
          { key: '/queue', label: '概览' },
          { key: '/queue/validate', label: '验证' },
        ],
      },
    ],
  },
];

export function buildMenuItems(): MenuProps['items'] {
  return navConfig.map((group) => ({
    type: 'group',
    label: group.label,
    children: group.children.map((item) => {
      if ('children' in item) {
        return {
          key: item.key,
          label: item.label,
          icon: item.icon,
          children: item.children.map((leaf) => ({ key: leaf.key, label: leaf.label })),
        };
      }
      return { key: item.key, label: item.label, icon: item.icon };
    }),
  }));
}

export function getOpenKeysFromPath(path: string): string[] {
  for (const group of navConfig) {
    for (const item of group.children) {
      if ('children' in item) {
        if (item.children.some((leaf) => path === leaf.key || path.startsWith(leaf.key + '/'))) {
          return [item.key];
        }
      }
    }
  }
  return [];
}

export function getBreadcrumb(selected: string) {
  const items: string[] = [];
  // try match from navConfig
  for (const group of navConfig) {
    for (const item of group.children) {
      if ('children' in item) {
        const leaf = item.children.find(
          (l) => selected === l.key || selected.startsWith(l.key + '/')
        );
        if (leaf) {
          items.push(group.label, item.label);
          if (
            leaf.key !== '/bst' &&
            leaf.key !== '/list' &&
            leaf.key !== '/stack' &&
            leaf.key !== '/queue'
          )
            items.push(leaf.label);
          return items.map((t) => ({ title: t }));
        }
      } else if (item.key === selected) {
        items.push(group.label, item.label);
        return items.map((t) => ({ title: t }));
      }
    }
  }
  // aux segmented mapping
  const map: Record<string, string> = { '/user': '用户', '/team': '团队', '/files': '文件' };
  if (map[selected]) {
    return [{ title: '辅助菜单' }, { title: map[selected] }];
  }
  return [{ title: '数据结构' }, { title: '首页' }];
}
